package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    //查询取消预约数量
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        LocalDateTime begin = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);//今天开始时间
        LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);//今天结束时间
        //查询指定时间段内，当前用户的取消次数
        return reservationMapper.countCancelledReservationsWithinTimeRange(begin, end, userId);
    }

    //查询每个时间段剩余预约次数
    @Override
    public List<TimeCountVo> countReservationsForEachTimeWithinTimeRange(LocalDateTime time) {
        LocalDateTime begin = time;//开始时间
        LocalDateTime end = begin.plusHours(24);//结束时间
        return reservationMapper.countReservationsForEachTimeWithinTimeRange(begin, end);
    }

    //新增预约
    @Override
    public void add(ReservationDto dto) {

        //获取登录用户id
        Long userId = UserThreadLocal.getUserId();

        //取消次数大于3次的也不能预约
        int cancelledReservationCount = getCancelledReservationCount(userId);
        if (cancelledReservationCount >=3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }

        //添加预约
        Reservation reservation = BeanUtil.copyProperties(dto, Reservation.class);

        reservation.setStatus(ReservationStatus.PENDING.getOrdinal());//设置状态
        reservation.setCreateBy(userId);//设置创建人
        try {
            reservationMapper.save(reservation);
        } catch (Exception e) {
            log.info(e + "");
            throw new BaseException(BasicEnum.TIME_ALREADY_RESERVATED_BY_PHONE);
        }
    }

    //分页查询预约
    @Override
    public PageResponse<ReservationVo> findByPage(Integer pageNum, Integer pageSize, Integer status) {

        PageHelper.startPage(pageNum,pageSize);

        List<Reservation> list = reservationMapper.findList(UserThreadLocal.getUserId(),status);
        Page<Reservation> page = (Page<Reservation>) list;

        return PageResponse.of(page, ReservationVo.class);
    }

    //取消预约
    @Override
    public void cancelReservation(Long id) {
        //设置id和状态
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(ReservationStatus.CANCELED.getOrdinal());
        //更新
        reservationMapper.update(reservation);
    }

}
